package com.example.mylibrary.testActivity.Basemodule;

import android.annotation.SuppressLint;
import android.annotation.TargetApi;
import android.content.ComponentName;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Build;
import android.text.TextUtils;

import androidx.annotation.RequiresApi;
import androidx.appcompat.app.AlertDialog;

public abstract class Permissions_Activity extends Base01_Activity {

    /**
     *
     * @param PERMISSIONS
     * @return  true 已经授权或者不需要  返回false 需要授权
     */
    public boolean setPermissions(String[] PERMISSIONS){
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            if (lacksPermissions(PERMISSIONS)){//读写权限没开启
                requestPermissions(PERMISSIONS,0x01);
            }else{
              return true;
            }
        }else{
            return true;
        }
       return false;
    }
    @RequiresApi(api = Build.VERSION_CODES.M)
    public void getPermissions(String[] PERMISSIONS){
        for(String Permissions:PERMISSIONS)
        {
            if(checkSelfPermission (Permissions)!= PackageManager.PERMISSION_GRANTED){
                dialogin();
                return;
            }
        }
    }
    /**
     *   在相应的页面接收回调
     * @param permissionsREAD
     * @return
     */
//    @RequiresApi(api = Build.VERSION_CODES.M)
//    @Override
//    public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
//        switch (requestCode)
//            case 0x01:
//                for(String Permissions:permissions){if(checkSelfPermission (Permissions)!= PackageManager.PERMISSION_GRANTED){dialogin();return; } }
//                //申请权限成功之后的操作


//                //申请权限成功之后的操作
//                break;
//        }
//    }

    @TargetApi(23)
    public  boolean lacksPermissions(String[] permissionsREAD) {
        for (String permission : permissionsREAD) {
            if (lacksPermission(permission)) {
                return true;
            }
        }
        return false;
    }
    /**
     * 判断是否缺少权限
     */
    @SuppressLint("WrongConstant")
    @TargetApi(23)
    private  boolean lacksPermission(String permission1) {
        return checkSelfPermission (permission1) == PackageManager.PERMISSION_DENIED;
    }
    public void dialogin() {
        //    通过AlertDialog.Builder这个类来实例化我们的一个AlertDialog的对象
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        //    设置Title的图标
//        builder.setIcon(R.drawable.ic_launcher);
        //    设置Title的内容
        builder.setTitle("权限未打开");
        //    设置Content来显示一个信息
        builder.setMessage("是否打开权限？");
        //    设置一个PositiveButton
        builder.setPositiveButton("确定", new DialogInterface.OnClickListener()
        {
            @Override
            public void onClick(DialogInterface dialog, int which)
            {
                initer();
                dialog.dismiss();
            }
        });
        //    设置一个NegativeButton
        builder.setNegativeButton("取消", new DialogInterface.OnClickListener()
        {
            @Override
            public void onClick(DialogInterface dialog, int which)
            {
                finish();
            }
        });
        //    显示出该对话框
        builder.show();
    }
    public void initer() {
        String sdk = Build.VERSION.SDK; // SDK号
        String model = Build.MODEL; // 手机型号
        String release = Build.VERSION.RELEASE; // android系统版本号
        String brand = Build.BRAND;//手机厂商
        if (TextUtils.equals(brand.toLowerCase(), "redmi") || TextUtils.equals(brand.toLowerCase(), "xiaomi")) {
            gotoMiuiPermission();//小米
        }
        else if (TextUtils.equals(brand.toLowerCase(), "huawei") || TextUtils.equals(brand.toLowerCase(), "honor")) {
            gotoHuaweiPermission();
        } else {
            startActivity(getAppDetailSettingIntent());
        }
    }
    /**
     * 跳转到miui的权限管理页面
     */
    private void gotoMiuiPermission() {
        try { // MIUI 8
            Intent localIntent = new Intent("miui.intent.action.APP_PERM_EDITOR");
            localIntent.setClassName("com.miui.securitycenter", "com.miui.permcenter.permissions.PermissionsEditorActivity");
            localIntent.putExtra("extra_pkgname", getPackageName());
            startActivity(localIntent);
        } catch (Exception e) {
            try { // MIUI 5/6/7
                Intent localIntent = new Intent("miui.intent.action.APP_PERM_EDITOR");
                localIntent.setClassName("com.miui.securitycenter", "com.miui.permcenter.permissions.AppPermissionsEditorActivity");
                localIntent.putExtra("extra_pkgname", getPackageName());
                startActivity(localIntent);
            } catch (Exception e1) { // 否则跳转到应用详情
                startActivity(getAppDetailSettingIntent());
            }
        }
    }
    /**
     * 华为的权限管理页面
     */
    private void gotoHuaweiPermission() {
        try {
            Intent intent = new Intent();
            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            ComponentName comp = new ComponentName("com.huawei.systemmanager", "com.huawei.permissionmanager.ui.MainActivity");//华为权限管理
            intent.setComponent(comp);
            startActivity(intent);
        } catch (Exception e) {
            e.printStackTrace();
            startActivity(getAppDetailSettingIntent());
        }
    }
    /**
     * 获取应用详情页面intent（如果找不到要跳转的界面，也可以先把用户引导到系统设置页面）
     *
     * @return
     */
    private Intent getAppDetailSettingIntent() {
        Intent localIntent = new Intent();
        localIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        if (Build.VERSION.SDK_INT >= 9) {
            localIntent.setAction("android.settings.APPLICATION_DETAILS_SETTINGS");
            localIntent.setData(Uri.fromParts("package", getPackageName(), null));
        } else if (Build.VERSION.SDK_INT <= 8) {
            localIntent.setAction(Intent.ACTION_VIEW);
            localIntent.setClassName("com.android.settings", "com.android.settings.InstalledAppDetails");
            localIntent.putExtra("com.android.settings.ApplicationPkgName", getPackageName());
        }
        return localIntent;
    }


}
